/*
 * @Author       : NaN 353362604@qq.com
 * @Date         : 2023-01-12 09:35:49
 * @LastEditTime : 2023-04-28 15:09:47
 * @LastEditors  : NaN 353362604@qq.com
 * @FilePath     : \AdminStration\src\Common\Modal\MoreModal\TreeModal.jsx
 * @Description  :
 */

import { Button, Tree, Spin, message } from 'antd'
import { useRequest, useBoolean } from 'ahooks'
import { useEffect, useState } from 'react'
import { Http } from '../../Api'

export const RolesTreeModal = ({ CurrentData }) => {
    const { get, getMenu, pos, uuid } = CurrentData
    const [TreeData, setTreeData] = useState()
    const [TreeSelectData, setTreeSelectData] = useState()
    const [HalfCheckedKeys, setHalfCheckedKeys] = useState([])
    const [loading, { setTrue, setFalse }] = useBoolean(true)
    const { runAsync: posRolesMenus } = useRequest(Http, { manual: true })
    const { runAsync: getRolesMenus } = useRequest(Http, {
        manual: true,
        onSuccess: res => {
            setTreeSelectData(res)
            setFalse()
        },
    })
    const { runAsync: getMenuWebTree } = useRequest(Http, {
        manual: true,
        onSuccess: res => {
            setTreeData(res)
            getRolesMenus({ url: get, data: { RoleUuid: uuid }, path: [uuid] })
        },
    })
    useEffect(() => {
        getMenuWebTree({ url: getMenu })
    }, [getMenu, getMenuWebTree])
    const onSubmit = () => {
        setTrue()
        posRolesMenus({ url: pos, data: [...TreeSelectData, ...HalfCheckedKeys], path: [uuid] }).then(() => {
            message.success('权限修改成功!')
            setFalse()
        })
    }
    return (
        <>
            {TreeSelectData && TreeData && (
                <div className="RolesTreeModal">
                    <Spin spinning={loading}>
                        <Tree
                            checkable
                            checkedKeys={TreeSelectData}
                            fieldNames={{ title: 'tableName', key: 'uuid' }}
                            onCheck={(e, { halfCheckedKeys }) => {
                                setTreeSelectData(e)
                                setHalfCheckedKeys(halfCheckedKeys)
                            }}
                            treeData={TreeData}
                        />
                        <Button type="primary" onClick={onSubmit}>
                            保存角色权限
                        </Button>
                    </Spin>
                </div>
            )}
        </>
    )
}
